﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IslandInterloper.Engine
{
    internal class IslandCommodity:GameClient
    {
        private string commodity = string.Empty;
        private double supply = 0.0;
        public double Supply
        {
            get
            {
                return supply;
            }
        }
        private double demand = 0.0;
        public double Demand
        {
            get
            {
                return demand;
            }
        }
        public double SellUnitPrice
        {
            get
            {
                return Game.Commodities[commodity].BasePrice * Demand / Supply;
            }
        }
        public double BuyUnitPrice
        {
            get
            {
                return SellUnitPrice * (1 + Game.Commodities[commodity].Markup);
            }
        }
        public void BuyUnits(double theUnits)
        {
            demand += Game.Commodities[commodity].DemandFactor * theUnits;
        }
        public void SellUnits(double theUnits)
        {
            supply += Game.Commodities[commodity].SupplyFactor * theUnits;
        }
        internal IslandCommodity(Game theGame, string theCommodity)
            : base(theGame)
        {
            commodity = theCommodity;
            supply = Game.Commodities[theCommodity].GenerateSupply();
            demand = Game.Commodities[theCommodity].GenerateDemand();
        }
        internal IslandCommodity(Game theGame, XElement element)
            : base(theGame)
        {
            FromXElement(element);
        }
        public void FromXElement(XElement element)
        {
            commodity = element.Name.LocalName;
            supply = Utilities.XElementToDouble(element.Element(GameXNames.supply));
            demand = Utilities.XElementToDouble(element.Element(GameXNames.demand));
        }
        public override XElement ToXElement(XName xName)
        {
            XElement result = new XElement(xName,
                new XElement(GameXNames.supply, supply.ToString()),
                new XElement(GameXNames.demand, demand.ToString())
                );
            return result;
        }
    }
}
